package edu.request;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Enumeration;

/**
 * 获取请求报头的名字
 * Enumeration getHeaderNames() 获得所有的请求报头的名称
 * String getHeader ( String headerName) ： 一个通用的根据请求报头的名称 获得取值的方法
 */
@WebServlet("/getRequestHeader")
public class GetRequestHeader extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Enumeration<String> headerNames = request.getHeaderNames();
        while (headerNames.hasMoreElements()){
            String s = headerNames.nextElement();
            System.out.println(s+"==>"+request.getHeader(s));
        }
        /**
         * 输出结果
         * Origin==>http://localhost
         * Cookie==>Idea-1583cd74=dd1e5a7a-f7e9-4b41-97f3-41803f78ed06; Webstorm-d51ef0c3=6f72a49a-f2fa-4bca-87ae-f35e13dbb579; JSESSIONID=node0108uf4xjjqmfoiq3afg187if11.node0
         * Accept==>text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,/*;q=0.8,application/signed-exchange;v=b3;q=0.9
         * Connection==>keep-alive
         * User-Agent==>Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.102 Safari/537.36 Edg/85.0.564.51
         * Referer==>http://localhost/
         * Sec-Fetch-Site==>same-origin
         * Sec-Fetch-Dest==>document
         * Host==>localhost
         * Accept-Encoding==>gzip, deflate, br
         * Sec-Fetch-Mode==>navigate
         * Cache-Control==>max-age=0
         * Upgrade-Insecure-Requests==>1
         * Sec-Fetch-User==>?1
         * Accept-Language==>zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6
         * Content-Length==>0
         * Content-Type==>application/x-www-form-urlencoded
         */
    }
}
